package com.castle.fortress.admin.cms.fragment;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.cms.dto.CmsSubjectDto;
import com.castle.fortress.admin.cms.service.CmsSubjectService;
import com.castle.fortress.admin.utils.SpringUtils;
import org.beetl.core.tag.GeneralVarTagBinding;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * cms 专题标签
 * @author castle
 */
public class CmsSpecialTag extends GeneralVarTagBinding {

	@Override
	public void render() {
		Map<String,Object> map = new HashMap<>();
		map.put("showflag",1);
		//指定专题编码
		if(containHtmlAttribute("code")){
			map.put("code",getAttributeValue("code"));
		}
		//过滤栏目数目
		if(containHtmlAttribute("row")&&StrUtil.isNotEmpty(getAttributeValue("row").toString())){
			try {
				Integer rows = Integer.valueOf(getAttributeValue("row").toString());
				map.put("row",rows);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		CmsSubjectService subjectService = SpringUtils.getBean(CmsSubjectService.class);
		List<CmsSubjectDto> list = subjectService.cmsTagListCmsSpecial(map);
		//初始化访问路径
		for(CmsSubjectDto t:list){
			if(StrUtil.isEmpty(t.getUrl())){
				t.setUrl("/page/"+t.getCode());
			}
		}
		this.binds(list);
		this.doBodyRender();
	}
}
